<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>
    <h1>修改商品页面</h1>
    <input type="text" v-model="p.id" placeholder="商品id" readonly>
    <input type="text" v-model="p.title" placeholder="商品标题">
    <input type="text" v-model="p.price" placeholder="商品价格">
    <input type="text" v-model="p.num" placeholder="商品库存">
    <input type="button" value="修改" @click="update()">
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
<script>
    let v = new Vue({
        el:"body>div",
        data:{
            p:{
                id:"",title:"",price:"",num:""
            }
        },
        methods:{
            update(){
                //发出异步修改post请求,因为传递的参数是一个对象所以post请求方便
                axios.post("/update",v.p).then(function () {
                    alert("修改完成!");
                    location.href="/list.html";//回到列表页面
                })
            }
        },
        created:function () {
            //进入页面就要发请求的代码写在此处
            //   /selectById?id=5
            //location.search = ?id=5
            //通过修改商品的id查询商品的详细信息
            axios.get("/selectById"+location.search).then(function (response) {
                //服务器返回的是装着商品详细信息的JSON字符串,axios框架会自动
                //将JSON字符串转成JS对象
                v.p = response.data;
            })
        }
    })
</script>
</body>
</html>